package com.caigou.modules.query.ui.sense

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.query.adpter.BoSeItemAdapter
import com.caigou.modules.query.bean.BoSeBallBean
import com.caigou.modules.query.databinding.FragmentBoSeBinding
import com.caigou.modules.query.viewmodel.SenseViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/01/03
 */
@AndroidEntryPoint
class BoSeFragment : BaseFragment() {

    private var _binding: FragmentBoSeBinding? = null
    private val binding get() = _binding!!


    private val senseViewModel: SenseViewModel by viewModels()

    private val redAdapter: BoSeItemAdapter by lazy {
        BoSeItemAdapter(1, mutableListOf())
    }

    private val blueAdapter: BoSeItemAdapter by lazy {
        BoSeItemAdapter(2, mutableListOf())
    }


    private val greenAdapter: BoSeItemAdapter by lazy {
        BoSeItemAdapter(3, mutableListOf())
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentBoSeBinding.inflate(inflater, container, false)
        return _binding?.root

    }

    override fun initView() {
        binding.boSeRecyclerViewRed.run {
            layoutManager = GridLayoutManager(requireActivity(), 7)
            adapter = redAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }

        binding.boSeRecyclerViewBlue.run {
            layoutManager = GridLayoutManager(requireActivity(), 7)
            adapter = blueAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }

        binding.boSeRecyclerViewGreen.run {
            layoutManager = GridLayoutManager(requireActivity(), 7)
            adapter = greenAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }
    }

    override fun lazyLoad() {
        senseViewModel.getBoSeBallList()
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(senseViewModel.boSeBallListLiveData, ::handleGetBoSeBallList)
    }

    private fun handleGetBoSeBallList(result: Resource<BoSeBallBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取波色号码异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                result.data?.apply {
                    redAdapter.setList(redNumberList)
                    blueAdapter.setList(blueNumberList)
                    greenAdapter.setList(greenNumberList)

                }
            }
        }
    }
}